﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Math;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Collision;

using ProceduralGolf.geometry;

namespace ProceduralGolf.physics
{
    class SphereObject : PhysicsObject
    {
        SpherePrimitive primitive;

        public float Radius { get { return radius; } }
        float radius;

        public SphereObject(Game game, Vector3 position, float radius)
            : base(position, Vector3.One * radius)
        {
            this.radius = radius;

            body = new Body();
            skin = new CollisionSkin(body);
            Sphere sphere = new Sphere(Vector3.Zero, radius);
            skin.AddPrimitive(sphere, new MaterialProperties(0.75f, 0.5f, 0.5f));
            body.CollisionSkin = skin;

            Vector3 com = SetMass(10.0f);

            body.MoveTo(position + com, Matrix.Identity);
            //skin.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            body.EnableBody();

            primitive = new SpherePrimitive(game.GraphicsDevice, radius, 8);
        }

        ~SphereObject()
        {
            primitive.Dispose();
        }

        public void Draw(Effect effect)
        {
            primitive.Draw(effect);
        }

        public void Draw(Matrix world, Matrix view, Matrix projection, Color color)
        {
            Matrix myWorld = world * GetWorldMatrix();
            primitive.Draw(myWorld, view, projection, color);
        }
    }
}
